package com.dxyxx.framework;

import org.apache.http.Header;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.dxyxx.R;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.pojo.ResponseResult;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class CAS {
	public CallBack mc;
	private Context context;
	private int requestCode;
	protected static ObjectMapper objectMapper = new ObjectMapper();

	public CAS(Context context, int requestCode, CallBack mc) {
		this.mc = mc;
		this.requestCode = requestCode;
		this.context = context;
	}

	public void call() {
		// 启动 等待框架
		final ProgressDialog progressDialog = ProgressDialog.show(context, null, null);
		progressDialog.setContentView(R.layout.dialog_circle);
		// 先访问 看看，看看 能不能得到 信息
		String urlString = "user/verify";
		HttpUtil.get(urlString, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				AgainPlayer.makeCall((Activity) context, "网络好像出错了");
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

				try {
					ResponseResult rr = objectMapper.readValue(new String(arg2), ResponseResult.class);
					if (rr.getCode() == -1 && rr.getInfo().equals("没有登陆")) { // 传递
						Intent intent = new Intent(context, LoginActivity.class);
						intent.putExtra("relogin", true);
						Toast.makeText(context, " 重新获取token", 0).show();
						((Activity) context).startActivityForResult(intent, requestCode);
					} else {// token正常
						mc.method();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFinish() {
				progressDialog.dismiss();
				super.onFinish();
			}
		});

	}

	public interface CallBack {
		public void method();

	}
}
